package com.shelpe.services.simbasync.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Util {

	public static final long MilliSecondsOfDay = 24*60*60*1000;
	
	public static final long MilliSecondsOfMin = 60*1000;
	
	public static final int MaxUriLength = 650;//uri长度限制，经验值
	
	public static Map<String, String> getDateRange(Date lastDate){
		
		Map<String, String> map = new HashMap<String, String>();
		
		Date dateNow = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateNowStr = sdf.format(dateNow);
		Date dateNowFormat = new Date();
		try {
			dateNowFormat = sdf.parse(dateNowStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(lastDate != null){
			long hitLimit = dateNow.getTime() - lastDate.getTime();
			if(hitLimit > MilliSecondsOfDay * 90){
				lastDate = null;
			}
		}
		
		Calendar calendar = Calendar.getInstance(); 
		if(lastDate != null){
			calendar.setTime(lastDate); 
			calendar.add(Calendar.HOUR_OF_DAY,24);//增加一天
		}else{
			calendar.setTime(dateNowFormat); 
			calendar.add(Calendar.HOUR_OF_DAY,-24*89);
		}
		Date startTime = calendar.getTime();
		map.put("startTime", sdf.format(startTime));
		
		calendar.setTime(dateNowFormat); 
		calendar.add(Calendar.HOUR_OF_DAY,-24);//减一天
		Date endTime = calendar.getTime();
		map.put("endTime", sdf.format(endTime));
		
		long durTime = endTime.getTime() - startTime.getTime();
		
		if(durTime > MilliSecondsOfDay*10){
			map.put("pageSize", "10000");
		}else{
			map.put("pageSize", "5000");
		}
		
		return map;
	}
	
	public static Date getStartTime(Date lastTime){
		
		Calendar calendar = Calendar.getInstance(); 
		
		if(lastTime == null){//返回90天的订购
			calendar.add(Calendar.HOUR_OF_DAY,-24*89);
			
			return calendar.getTime();
		}
		
		calendar.setTime(lastTime);
		calendar.add(Calendar.SECOND, 1);//加一秒
		
		return calendar.getTime();
	}
	
	public static void adjustTaskRange(Map<String, String> map){
		
		String startDateStr = map.get("startTime");
		String endDateStr = map.get("endTime");
		
		try {
			
			if(startDateStr.compareTo(endDateStr) < 1){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Date endDate = sdf.parse(endDateStr);
				Calendar calendar = Calendar.getInstance(); 
				calendar.setTime(endDate); 
				calendar.add(Calendar.HOUR_OF_DAY,-24*15);
				String tempStartDate = sdf.format(calendar.getTime());
				if(tempStartDate.compareTo(startDateStr) < 0){
					map.put("startTime", tempStartDate);
				}
			}

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void getTaskRange(Date lastDate, String keywordStr, Map<String, List<String>> map){
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date dateNow = new Date();
		String dateNowStr = sdf.format(dateNow);
		Date dateNowFormat = new Date();;
		try {
			dateNowFormat = sdf.parse(dateNowStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(lastDate != null){
			long hitLimit = dateNow.getTime() - lastDate.getTime();
			if(hitLimit > MilliSecondsOfDay * 30){//最多获取一个月的数据
				lastDate = null;
			}
		}
		
		Calendar calendar = Calendar.getInstance(); 
		if(lastDate != null){
			calendar.setTime(lastDate); 
			calendar.add(Calendar.HOUR_OF_DAY,24);//增加一天
		}else{
			calendar.setTime(dateNowFormat); 
			calendar.add(Calendar.HOUR_OF_DAY,-24*7);//获取一个星期的数据
		}
		
		Date startTime = calendar.getTime();
		
		calendar.setTime(dateNowFormat); 
		calendar.add(Calendar.HOUR_OF_DAY,-24);//减一天
		Date endTime = calendar.getTime();
		calendar.setTime(startTime); 
		
		while(startTime.getTime() <= endTime.getTime()){
			String dateStr = sdf.format(startTime);
			if(map.containsKey(dateStr)){
				map.get(dateStr).add(keywordStr);
			}else{
				List<String> wordList = new ArrayList<String>();
				wordList.add(keywordStr);
				map.put(dateStr, wordList);
			}
			
			calendar.add(Calendar.HOUR_OF_DAY,24);
			startTime = calendar.getTime();
		}
	}
	
	public static Map<String, String> getTaskRange(){
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date dateNow = new Date();
		String dateNowStr = sdf.format(dateNow);
		Date dateNowFormat = new Date();;
		try {
			dateNowFormat = sdf.parse(dateNowStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Calendar calendar = Calendar.getInstance(); 
		
		calendar.setTime(dateNowFormat); 
		calendar.add(Calendar.HOUR_OF_DAY,-24*3);//三天的数据
		Date startDate = calendar.getTime();
		
		calendar.setTime(dateNowFormat); 
		calendar.add(Calendar.HOUR_OF_DAY,-24);//减一天
		Date endDate = calendar.getTime();
		
		Map<String, String> map = new HashMap<String, String>();
		
		map.put("startDate", sdf.format(startDate));
		map.put("endDate", sdf.format(endDate));
		
		return map;
	}
	
	public static String createKey(String mode, String userId, String campaignId, String adgroupId, String endDate){
		
		StringBuilder sb = new StringBuilder();
		
		if(mode != null){
			sb.append(mode);
		}
		
		if(userId != null){
			sb.append(userId);
		}
		
		if(campaignId != null){
			sb.append(campaignId);
		}
		
		if(adgroupId != null){
			sb.append(adgroupId);
		}
		
		if(endDate != null){
			sb.append(endDate);
		}
		
		return sb.toString();
		
	}
	public static Date getLastDayYYYYMMDD(){
		
		Date dateNow = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateNowStr = sdf.format(dateNow);
		Date dateNowFormat = new Date();
		
		try {
			dateNowFormat = sdf.parse(dateNowStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Calendar calendar = Calendar.getInstance(); 
		calendar.setTime(dateNowFormat); 
		calendar.add(Calendar.HOUR_OF_DAY,-24);
		
		return calendar.getTime();	
	}
	
	public static Date getYYYYMMDD(Date dateTime){
		
		Date dateNow = dateTime;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateNowStr = sdf.format(dateNow);
		
		try {
			dateNow = sdf.parse(dateNowStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return dateNow;
	}
	
}